/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.action;

import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.util.List;
import java.util.Map;

import javacommon.util.ResInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.org.rapid_framework.beanutils.BeanUtils;
import cn.org.rapid_framework.web.scope.Flash;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.tech15.pub.model.*;
import com.tech15.pub.dao.*;
import com.tech15.pub.service.*;
import com.tech15.pub.vo.query.*;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class PubDqgzAction extends BaseStruts3Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String LIST_JSP= "/pub/pubDqgz_list.jsp";
	protected static final String CREATE_JSP = "/pub/pubDqgz_add.jsp";
	protected static final String EDIT_JSP = "/pub/pubDqgz_edit.jsp";
	protected static final String SHOW_JSP = "/pub/pubDqgz_show.jsp";
	
	protected static final String SEARCH_DQGZ_JSP = "/pub/pub_dqgz.jsp";
	protected static final String DETAIL_JSP = "/pub/pub_dqgz_detail.jsp";

	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pub/list.do";
	private static final String SEARCH_DQGZ2_JSP = "/pub/pub_dqgz2.jsp";
	
	private PubDqgzManager pubDqgzManager;
	private PubDataManager pubDataManager;
	private PubDqgz pubDqgz;
	java.lang.String id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			pubDqgz = new PubDqgz();
		} else {
			pubDqgz = (PubDqgz)pubDqgzManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setPubDqgzManager(PubDqgzManager manager) {
		this.pubDqgzManager = manager;
	}	
	public void setPubDataManager(PubDataManager manager) {
		this.pubDataManager = manager;
	}
	
	public Object getModel() {
		return pubDqgz;
	}
	
	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		PubDqgzQuery query = newQuery(PubDqgzQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = pubDqgzManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	

	public String dqgz() {
		PubDqgzQuery query = newQuery(PubDqgzQuery.class,DEFAULT_SORT_COLUMNS);
		
		List<PubData> lbList = pubDataManager.searchByPid(new Long(161));
		getRequest().setAttribute("lbList", lbList);
		
		if(ObjectUtils.isEmpty(query.getZt())){
			query.setZt(new Long(162));
			pubDqgz.setZt(new Long(162));
		}
		
		if(ObjectUtils.isNotEmpty(query.getZt())){
			PubData pd = pubDataManager.getById(query.getZt());
			if(pd != null){
				this.pubDqgz.setDataname(pd.getName());
			}
		}
		
		Map map = pubDqgzManager.findPageDqgz(query);
		getRequest().setAttribute("dqgzmap", map);
		
		return SEARCH_DQGZ_JSP;
	}
	
	public String dqgz2() {
		PubDqgzQuery query = newQuery(PubDqgzQuery.class,DEFAULT_SORT_COLUMNS);
		
		List<PubData> lbList = pubDataManager.searchByPid(new Long(161));
		getRequest().setAttribute("lbList", lbList);
		
		if(ObjectUtils.isNotEmpty(query.getZt())){
			PubData pd = pubDataManager.getById(query.getZt());
			if(pd != null){
				this.pubDqgz.setDataname(pd.getName());
			}
		}
		
		Page page = pubDqgzManager.findPage(query);
		savePage(page,query);
		return SEARCH_DQGZ2_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	public String detail() {
		List<PubData> lbList = pubDataManager.searchByPid(new Long(161));
		getRequest().setAttribute("lbList", lbList);
		return DETAIL_JSP;
	}
	
	/** 保存新增对象 */
	public void save() {
		Map<String, Object> resInfo = null;
		try{
			pubDqgz.setFbr(this.getLoginInfo().getUserid());
			pubDqgzManager.saveInfo(pubDqgz);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**进入更新页面*/
	public String edit() {
		if(!this.isNullOrEmptyString(this.pubDqgz.getZt())){
			List<PubData> lbList = pubDataManager.searchByPid(this.pubDqgz.getZt());
			getRequest().setAttribute("lbList", lbList);
		}
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			pubDqgzManager.updateInfo(this.pubDqgz);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try {
			pubDqgzManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
}
